package com.example.spingbootvue.controller;

import com.example.spingbootvue.entity.User;
import com.example.spingbootvue.entity.UseraCommunity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;
import com.example.spingbootvue.service.communityservice;
import com.example.spingbootvue.mapper.UserCommunityMapper;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Community")
@ComponentScan("application.yml")
public class CommunityController {

    @Autowired
    communityservice communityservice;

    @Autowired
    UserCommunityMapper userCommunityMapper;

    //查询所有用户发布的动态信息
    @GetMapping("/selectAll")
    public List<User> selectAll() {
        return userCommunityMapper.getUser();
    }

    //查询当前用户发布的动态信息
    @GetMapping("/selectAllbysickname")
    public List<User> selectAllbysickname(@RequestParam String sickname) {
        return userCommunityMapper.getUserBySickname(sickname);
    }

    //查询用户发布的动态别人评论的信息
    @GetMapping("/selectUserComment/{foreignid}")
    public List<UseraCommunity> selectUserComment(@PathVariable Integer foreignid) {
        return userCommunityMapper.getUserComment(foreignid);
    }

    //插入发布的动态的信息
    @PostMapping("/insertDtMsg")
    public int insertDtMsg(@RequestBody User user) {
        return userCommunityMapper.insertDtMsg(user.getAccount(),user.getSickname(),user.getContent(),user.getImage(),user.getPosttime(),user.getCommentimage());
    }

    //插入发布的评论的信息
    @PostMapping("/insertPlMsg")
    public int insertPlMsg(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.insertPlMsg(useraCommunity.getForeignid(),useraCommunity.getSickname(),useraCommunity.getImage(),useraCommunity.getReplycontent(),useraCommunity.getReplytime(),useraCommunity.getReplysickname(),useraCommunity.getAccount());
    }

    //将发布动态信息的pid与评论信息中pid进行绑定
    @PutMapping("/updatePid")
    public int updatePid(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.updatePid(useraCommunity.getPid(),useraCommunity.getForeignid());
    }

    //查询此条动态有几条评论
    @GetMapping("/CommentCount/{foreignid}")
    public int CommentCount(@PathVariable Integer foreignid) {
        return userCommunityMapper.countComment(foreignid);
    }

    //动态点赞,点击点赞按钮点赞数量加一
    @PutMapping("/addLikenum/{foreignid}")
    public int addLikenum(@PathVariable Integer foreignid) {
        return userCommunityMapper.addLikenum(foreignid);
    }

    //动态点赞,当用户已经点赞,再次点击点赞按钮点赞数量减一
    @PutMapping("/minusLikenum/{foreignid}")
    public int minusLikenum(@PathVariable Integer foreignid) {
        return userCommunityMapper.minusLikenum(foreignid);
    }

    //用户点击删除可以将自己发布的动态删除
    @DeleteMapping("/deleteDtMsg/{foreignid}")
    public int deleteDtMsg(@PathVariable Integer foreignid) {
        return userCommunityMapper.deleteDtMsg(foreignid);
    }

    //插入回复其他用户的评论
    @PostMapping("/ReplyComment")
    public int ReplyComment(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.ReplyComment(useraCommunity.getForeignid(),useraCommunity.getSickname(),useraCommunity.getImage(),
                useraCommunity.getReplycontent(),useraCommunity.getReplytime(),useraCommunity.getPid(),useraCommunity.getReplysickname(),useraCommunity.getAccount());
    }

    //获取被回复的用户昵称
//    @GetMapping("/getSickname/{foreignid}")
//    public List<UseraCommunity> getSickname(@PathVariable Integer foreignid) {
//        return userCommunityMapper.getSickname(foreignid);
//    }

    //用户在搜索框中输入关键字进行搜索
    @GetMapping("/getUserByContentLike/{content}")
    public List<User> getUserByContentLike(@PathVariable String content) {
        return  userCommunityMapper.getUserByContentLike(content);
    }

    //个人主页编辑用户输入昵称可以更新
    @PutMapping("/updatesickname1")
    public int updatesickname1(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.updatesickname1(useraCommunity.getSickname(),useraCommunity.getAccount());
    }

    @PutMapping("/updatesickname2")
    public int updatesickname2(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.updatesickname2(useraCommunity.getSickname(),useraCommunity.getAccount());
    }

    @PutMapping("/updatesickname3")
    public int updatesickname3(@RequestBody UseraCommunity useraCommunity) {
        return userCommunityMapper.updatesickname3(useraCommunity.getSickname(),useraCommunity.getAccount());
    }

    @PutMapping("/updatesickname4")
    public int updatesickname4() {
        return userCommunityMapper.updatesickname4();
    }
}
